package com.kuaimacode.kframework.api.controller;

import com.kuaimacode.kframework.api.service.AgentService;
import com.kuaimacode.kframework.api.service.ContentService;
import com.kuaimacode.kframework.api.service.HelpService;
import com.kuaimacode.kframework.api.service.MasterOrderAccitityService;
import com.kuaimacode.kframework.api.service.user.OrderService;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.MasterConstants;
import com.kuaimacode.kframework.enums.ProtocolTypeEnum;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgent;
import com.kuaimacode.kframework.mybatis.models.category.AgentServ;
import com.kuaimacode.kframework.mybatis.models.master.MasterOrderAccitity;
import com.kuaimacode.kframework.mybatis.models.user.Order;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * <p>
 * 内容表（相关协议、说明、关于我们、客服电话等） 前端控制器
 * </p>
 *
 * @author Bean
 * @since 2019-03-15
 */
@RestController
@RequestMapping("/content")
@Api(tags = "内容管理相关",description = "内容管理相关")
public class ContentController {



    @Autowired
    private ContentService contentService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private AgentService agentService;

    @Autowired
    private HelpService helpService;

    @Autowired
    private MasterOrderAccitityService accitityService;

    @ApiOperation(value = "获取用户协议")
    @GetMapping("/getUserProtocol")
    public JsonResult getUserProtocol() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.USER_PROTOCOL));
    }


    @ApiOperation(value = "获取师傅协议")
    @GetMapping("/getMasterProtocol")
    public JsonResult getMasterProtocol() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.MASTER_PROTOCOL));
    }

    @ApiOperation(value = "获取隐私协议")
    @GetMapping("/getPrivacyProtocol")
    public JsonResult getPrivacyProtocol() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.PRIVACY_PROTOCOL));
    }

    @ApiOperation(value = "获取关于我们")
    @GetMapping("/getAboutUs")
    public JsonResult getAboutUs() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.ABOUT_US));
    }

    @ApiOperation(value = "获取优惠券说明")
    @GetMapping("/getCouponExplain")
    public JsonResult getCouponExplain() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.COUPON_EXPLAIN));
    }
    @ApiOperation(value = "获取随意购协议")
    @GetMapping("/getBuyProtocol")
    public JsonResult getBuyProtocol() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.BUY_PROTOCOL));
    }

    @ApiOperation(value = "获取保价协议")
    @GetMapping("/getPriceProtocol")
    public JsonResult getPriceProtocol() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.PRICE_PROTOCOL));
    }

    @ApiOperation(value = "获取危害说明")
    @GetMapping("/getHarmExplain")
    public JsonResult getHarmExplain() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.HARM_EXPLAIN));
    }


    @ApiOperation(value = "获取接单说明")
    @GetMapping("/getOrderExplain")
    public JsonResult getOrderExplain() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.ORDER_EXPLAIN));
    }


    @ApiOperation(value = "获取版本说明")
    @GetMapping("/getVersionExplain")
    public JsonResult getVersionExplain() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.VERSION_EXPLAIN));
    }

    @ApiOperation(value = "获取客服400电话")
    @GetMapping("/getPhoneNumber")
    public JsonResult getPhoneNumber() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.PHONE_NUMBER));
    }

    @ApiOperation(value = "获取注册协议与服务条款")
    @GetMapping("/getRegisterProtocol")
    public JsonResult getRegisterProtocol() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.REGISTER_PROTOCOL));
    }

    @ApiOperation(value = "获取用户充值协议")
    @GetMapping("/getRechargeProtocol")
    public JsonResult getRechargeProtocol() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.RECHARGE_PROTOCOL));
    }

    @ApiOperation(value = "获取提现说明")
    @GetMapping("/getDrawExplain")
    public JsonResult getDrawExplain() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.DRAW_EXPLAIN));
    }

    @ApiOperation(value = "获取使用须知")
    @GetMapping("/getUseProtocol")
    public JsonResult getUseProtocol() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.USE_PROTOCOL));
    }

    @ApiOperation(value = "获取帮助列表")
    @GetMapping("/queryHelpList")
    public JsonResult queryHelpList() {
        return JsonResult.success(helpService.queryHelpList());
    }

    @ApiOperation(value = "获取帮助详情")
    @GetMapping("/getHelpContent/{id}")
    @ApiImplicitParam(name = "id", value = "问题id", required = true, dataType = "long",paramType = "path")
    public JsonResult getHelpContent(@PathVariable Long id) {
        return JsonResult.success(helpService.getById(id));
    }

    @ApiOperation(value = "拒绝接单说明文案")
    @GetMapping("/getRefuseExplain")
    public JsonResult getRefuseExplain() {
        return JsonResult.success("操作成功", contentService.queryContentInfo(ProtocolTypeEnum.UN_ACCEPT_EXPLAIN));
    }
    @ApiOperation(value = "取消订单说明文案")
    @GetMapping("/getCancelOrderExplain/{orderId}")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "long",paramType = "path")
    public JsonResult getCancelOrderExplain(@PathVariable Long orderId) {
        //获取订单所属分站
        Order order = orderService.getById(orderId);

        if(order!=null){
            BigDecimal rate = agentService.getById(order.getAgentId()).getCancelOrder();
            if(BigDecimal.ZERO.compareTo(rate)!=-1){
                return JsonResult.success("操作成功","是否确认取消");
            }

            //查询当前订单的流程状态
            MasterOrderAccitity accitity = accitityService.getMasterAccitityByOrderId(order.getId(), null);
            BigDecimal orderAmount = order.getAmount();

            if(orderAmount.compareTo(BigDecimal.ZERO) <= 0){
                //全额优惠券抵扣的情况
                return JsonResult.success("操作成功","是否确认取消");
            }
            //flag  是否扣款
            boolean  flag = false;
            /*
             * 需要扣款 的节点：
             * 帮我送：我已到达（第二步完成）
             * 帮我取：我已到达
             * 随意购：我已到达（附近购买以终点我已到达为判断依据）
             * 万能帮办：第三步拍照完毕，开始服务为节点
             * 按比例扣除后师傅和分站平分
             *
             */
            if(order.getType() == Constant.OrderTypeEnum.PAO_TUI.getCode() && accitity !=null){
                //跑腿
                if(order.getChildType() == Constant.PaotuiTypeEnum.HELP_TAKE.getCode()){
                    //帮我送
                    if(Integer.parseInt(MasterConstants.MasterOrderCurrentStatus.STATUS_2.getCode())
                            < Integer.parseInt(accitity.getCurrentStatus())){
                        flag = true;
                    }
                }
                if(order.getChildType() == Constant.PaotuiTypeEnum.HELP_SEND.getCode()){
                    //帮我取
                    if(Integer.parseInt(MasterConstants.MasterOrderCurrentStatus.STATUS_2.getCode())
                            <= Integer.parseInt(accitity.getCurrentStatus())){
                        flag = true;
                    }
                }
                if(order.getChildType() == Constant.PaotuiTypeEnum.ANY_BUY.getCode()){
                    //随意购
                    if(Integer.parseInt(MasterConstants.MasterOrderCurrentStatus.STATUS_2.getCode())
                            <= Integer.parseInt(accitity.getCurrentStatus())){
                        flag = true;
                    }
                }

            }else if( accitity !=null){
                //技术服务
                if(Integer.parseInt(MasterConstants.MasterOrderCurrentStatus.STATUS_3.getCode())
                        < Integer.parseInt(accitity.getCurrentStatus())){
                    flag = true;
                }
            }

            if(flag){
                String explain =  contentService.queryContentInfo(ProtocolTypeEnum.CANCEL_ORDER_EXPLAIN);
                return JsonResult.success("操作成功",explain.replace("${rate}",rate+"%"));
            }else{
                return JsonResult.success("操作成功","是否确认取消");
            }


        }else{
            return JsonResult.failure("订单不存在");
        }

    }



}
